#define  _CRT_SECURE_NO_WARNINGS
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    ListNode* reverse(ListNode* head)
    {
        if (!head)return head;
        ListNode* prev = nullptr, * cur = head, * next = cur->next;
        while (cur)
        {
            next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
public:
    void reorderList(ListNode* head) {
        if (!head->next)return;
        ListNode* slow = head, * fast = head, * tail;
        while (fast && fast->next)
        {
            tail = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        tail->next = nullptr;

        ListNode* cur1 = head, * cur2 = reverse(slow);

        ListNode guard;
        tail = &guard;
        while (cur1 || cur2)
        {
            if (cur1)
            {
                tail->next = cur1;
                tail = tail->next;
                cur1 = cur1->next;
            }
            if (cur2)
            {
                tail->next = cur2;
                tail = tail->next;
                cur2 = cur2->next;
            }
        }
    }
};